﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

namespace Our.Umbraco.uDbCompare
{
    public partial class TemplateInUse : System.Web.UI.UserControl
    {
        public bool UseRemoteConnectionString { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindData();
        }

        private string connectionString
        {
            get
            {
                string connectionString = uDbCompare.CurrentConnectionString;
                if (UseRemoteConnectionString)
                    connectionString = uDbCompare.RemoteConnectionString;
                return connectionString;
            }
        }

        public void BindData()
        {
            if (uDbCompare.IsValidDbConnection(UseRemoteConnectionString))
            {
                DataTable templateTable = uDbCompare.QueryDb("select nodeId, alias from cmsTemplate", connectionString);
                TemplateDropDownList.DataTextField = "alias";
                TemplateDropDownList.DataValueField = "nodeId";
                TemplateDropDownList.DataSource = templateTable;
                TemplateDropDownList.DataBind();
            }
        }

        protected void TemplateButton_OnClick(object sender, EventArgs e)
        {
            DataTable matchDt = FindTemplate(TemplateDropDownList.SelectedValue);
            TemplateGridView.DataSource = matchDt;
            TemplateGridView.DataBind();

            NoResultsLabel.Visible = (matchDt.Rows.Count == 0);
        }

        private DataTable FindTemplate(string alias)
        {
            string templateQuery = @"select Template.alias as TemplateAlias, template.nodeid as TemplateNodeId, dt.IsDefault as IsDefaultTemplate, 
                                        ct.alias as ContentTypeAlias, ct.nodeId as ContentTypeNodeId, n.text as ContentNodeName, n.id as ContentNodeId
                                        from cmsTemplate Template
			                            left outer join umbracoNode TemplateNode on TemplateNode.id = Template.nodeId
			                            left outer join cmsDocumentType dt on dt.templateNodeId = TemplateNode.id 
			                            left outer join cmsContentType ct  on dt.contenttypenodeid = ct.nodeid
			                            left outer join cmscontent c on ct.nodeid = c.contenttype
			                            left outer join umbracoNode n on c.nodeid = n.id
                                        where templatenodeid = ?";
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@TemplateNodeId", TemplateDropDownList.SelectedValue); 
            DataTable dt = uDbCompare.QueryDb(templateQuery, connectionString, parameters);

            return dt;
        }
    }
}